package com.tushen.service.impl;


import com.tushen.mapper.CommonDao;
import com.tushen.mapper.DataProcessDao;
import com.tushen.service.CommonService;
import com.tushen.task.DatThread;
import com.tushen.task.NCThread;
import com.tushen.task.UVThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tushen.utils.CommonQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * create by likun on 2019/9/9 0009
 */
@Service
public class CommmonServiceImpl implements CommonService {

    @Autowired
    private CommonDao commonDao;

    @Autowired
    private DataProcessDao dataProcessDao;

    @Override
    public Map getDataBySql(String sql) {
        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        try {
            List<HashMap> list=commonDao.executeSql(sql);
            resultMap.put("data",list);
            resultMap.put("code",1);
            resultMap.put("msg","操作成功");
        }catch (Exception e){
            resultMap.put("code",-1);
            resultMap.put("msg",e.getLocalizedMessage());
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public List<HashMap> getDataListBySql(String sql) {
        return commonDao.executeSql(sql);
    }

    @Override
    public List<HashMap> getTable(String table) {
        return commonDao.getTableAll(table);
    }


    @Override
    public HashMap getQueryData(HashMap paramJson, String func) {
        return CommonQuery.getYunyingIndex(commonDao, paramJson, func);

    }

    @Override
    public HashMap processData(HashMap paramJson, String func) {
        HashMap map=CommonQuery.getYunyingIndex(commonDao, paramJson, func);
        List<HashMap> resultList=(List<HashMap>)map.get("data");
        if(resultList.size()>0){
            return map;
        }else {
            //任务处理
            int status=1;
            String uuid= UUID.randomUUID().toString();
            HashMap whereMap=new HashMap();
            whereMap.put("status",status);
            whereMap.put("taskid",uuid);
            whereMap.putAll(paramJson);
            String path=paramJson.get("path").toString();
            String filePath=paramJson.get("data_path").toString();
            String version=paramJson.get("version").toString();

            int n=dataProcessDao.insertProcessTask(whereMap);
            if(n>0){
                map=CommonQuery.getYunyingIndex(commonDao, paramJson, func);
            }
            switch (path){
                case "nc":
                    NCThread ncThread=new NCThread(filePath,uuid,version);
                    ncThread.start();
                    break;
                case "uv":
                    UVThread uvThread=new UVThread(filePath,uuid,version);// 三维浓度场入库
                    uvThread.start();
                    break;
                case "polcon":
                    DatThread datThread=new DatThread(filePath,uuid,version);
                    datThread.start();
                    break;
            }

        }
        return map;

    }





}
